/**
  * 排行榜
  */
module game {

    export class OpenRankingMediator extends BaseMediator {
        public static NAME: string = "OpenRankingMediator";
        public constructor(viewComponent: any = null) {
            super(OpenRankingMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.HALL.OPEN_RANKING,
                PanelNotify.HALL.CLOSE_RANKING
            ];
        }
        public handleNotification(notification: puremvc.INotification): void {
            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.panel) {
                        this.panel.width = GameConfig.curWidth()
                        this.panel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.HALL.OPEN_RANKING: {
                    this.panel = null;
                    this.panel = new OpenRankingPanel();
                    this.showUI(this.panel, 2, 1, true);
                    break;
                }
                case PanelNotify.HALL.CLOSE_RANKING: {
                    this.closePanel(0);
                    break;
                }
            }
        }
        private panel: OpenRankingPanel;
        public obj = null;
        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.panel.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeButtonClick, this)
            this.panel.todayRk.addEventListener(egret.TouchEvent.TOUCH_TAP, this.dateSettingClick, this)
            this.panel.theWeekRk.addEventListener(egret.TouchEvent.TOUCH_TAP, this.dateSettingClick, this)
            this.panel.lastWeekRk.addEventListener(egret.TouchEvent.TOUCH_TAP, this.dateSettingClick, this)
            this.panel.shuoming.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
                game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_HELP_PHB)
            }, this)
        }

        /**
         * 初始化面板数据
         */
        /**
 * 初始化面板数据
 */
        //当天，本周，上周分别查询
        private today = false
        private theweek = false
        private lastweek = false
        private dataList: Array<Object> = []
        private selfData: Object = {}
        public initData(): void {
            this.dateSetting()
        }
        private dateSetting(type: string = "getscorTodayhrank") {

            this.today = false;
            this.theweek = false;
            this.lastweek = false;
            this.dataList = [];
            var time = Global.getCurrTimeLong()
            var signature = Global.getSynLoginKey(time)
            signature = signature.toUpperCase()
            //查询线上
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/WS/PhoneRank.ashx?userid=" + GameVO.getVo().dwUserID + "&action=" + type + "&time=" + time + "&signature=" + signature + "&pageSize=" + 50 + "&pageIndex=" + 1, egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                EffectUtils.hideWaritPanel()
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    if (obj.code == 0) {
                        this.dataList = obj["ScoreRanks"];
                        this.selfData = obj["MyScore"];
                        this.today = true;
                        this.showData(type)
                    } else {
                        Global.tipShow(obj.msg)
                    }
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                EffectUtils.hideWaritPanel()
                Global.tipShow("网络请求失败，请稍后重试!")
            }, this);
            EffectUtils.showWaritPanel()
            request.send();
        }
        private selectButton: eui.Button = null;
        private dateSettingClick(event: egret.TouchEvent) {
            this.selectButton = event.currentTarget;
            if (this.selectButton == this.panel.theWeekRk) {
                this.dateSetting("getscoreWeekrank")
                this.panel.theweek_group.visible = true;
                this.panel.theWeekRk.enabled = false;
                this.panel.theWeekRk.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.dateSettingClick, this)
                this.panel.today_group.visible = false;
                this.panel.todayRk.enabled = true;
                this.panel.todayRk.addEventListener(egret.TouchEvent.TOUCH_TAP, this.dateSettingClick, this)
                this.panel.lastweek_group.visible = false;
                this.panel.lastWeekRk.enabled = true;
                this.panel.lastWeekRk.addEventListener(egret.TouchEvent.TOUCH_TAP, this.dateSettingClick, this)
            }
            if (this.selectButton == this.panel.lastWeekRk) {
                this.dateSetting("getscorePreDayrank")
                this.panel.lastweek_group.visible = true;
                this.panel.lastWeekRk.enabled = false;
                this.panel.lastWeekRk.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.dateSettingClick, this)
                this.panel.theweek_group.visible = false;
                this.panel.theWeekRk.enabled = true;
                this.panel.theWeekRk.addEventListener(egret.TouchEvent.TOUCH_TAP, this.dateSettingClick, this)
                this.panel.today_group.visible = false;
                this.panel.todayRk.enabled = true;
                this.panel.todayRk.addEventListener(egret.TouchEvent.TOUCH_TAP, this.dateSettingClick, this)
            } if (this.selectButton == this.panel.todayRk) {
                this.dateSetting("getscorTodayhrank")
                this.panel.today_group.visible = true;
                this.panel.todayRk.enabled = false;
                this.panel.todayRk.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.dateSettingClick, this)
                this.panel.theweek_group.visible = false;
                this.panel.theWeekRk.enabled = true;
                this.panel.theWeekRk.addEventListener(egret.TouchEvent.TOUCH_TAP, this.dateSettingClick, this)
                this.panel.lastweek_group.visible = false;
                this.panel.lastWeekRk.enabled = true;
                this.panel.lastWeekRk.addEventListener(egret.TouchEvent.TOUCH_TAP, this.dateSettingClick, this)

            }
        }

        private showData(type) {
            //先把所有订单生成时间转换成时间戳，然后按照时间戳排序
            var arr = this.dataList
            var leng = arr.length;
            if (type == "getscorTodayhrank") {
                this.panel.dataGroupToday.removeChildren()
                for (var i = 0; i < leng; i++) {
                    this.panel.dataGroupToday.addChild(new components.PHB_jilvPanel(arr[i], false))
                }
            }
            else if (type == "getscoreWeekrank") {
                this.panel.dataGroupThe.removeChildren()
                for (var i = 0; i < leng; i++) {
                    this.panel.dataGroupThe.addChild(new components.PHB_jilvPanel(arr[i], true))
                }
            }
            else if (type == "getscorePreDayrank") {
                this.panel.dataGroupLast.removeChildren()
                for (var i = 0; i < leng; i++) {
                    this.panel.dataGroupLast.addChild(new components.PHB_jilvPanel(arr[i], true))
                }
            }

            if (type == "getscorTodayhrank" || type == "getscoreWeekrank") {
                if (this.selfData["RankId"] > 50 || this.selfData["RankId"] == 0) {
                    this.panel.phbname.text = Global.cutString(this.selfData["UserName"])
                    this.panel.phbscore.text = this.selfData["SumScore"] / 100 + ""
                    this.panel.icon_right.source = "phb_wsb"
                    this.panel.icon.source = ""
                    this.panel.rankIdNum.text = ""
                    this.panel.icon_txk.source = "phb_txk";
                    this.panel.icon_tx.source = "special_" + this.selfData["FaceId"];
                }
                else if (this.selfData["RankId"] <= 50 && this.selfData["RankId"] > 3) {
                    this.panel.phbname.text = Global.cutString(this.selfData["UserName"])
                    this.panel.phbscore.text = this.selfData["SumScore"] / 100 + ""
                    this.panel.icon_right.source = ""
                    this.panel.icon.source = ""
                    this.panel.rankIdNum.text = "" + this.selfData["RankId"]
                    this.panel.icon_txk.source = "phb_txk"
                    this.panel.icon_tx.source = "special_" + this.selfData["FaceId"];
                }
                else {
                    this.panel.phbname.text = Global.cutString(this.selfData["UserName"])
                    this.panel.phbscore.text = this.selfData["SumScore"] / 100 + ""
                    this.panel.icon_right.source = ""
                    this.panel.icon.source = "phb_nob" + this.selfData["RankId"]
                    this.panel.rankIdNum.text = ""
                    this.panel.icon_txk.source = "phb_txk"
                    this.panel.icon_tx.source = "special_" + this.selfData["FaceId"]
                }
                this.panel.selfrankingToday.visible = true;
            }
            else {
                this.panel.selfrankingToday.visible = false;
            }

        }
        private closeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            this.closePanel(1);
        }

    }
}
